import java.util.Iterator;
import java.util.Vector;


public class StevesChunkGetter 
{
	
	//WARNING THESE MUST BE SINGLE CHARACTERS
	char c1;
	char c2;
	
	public StevesChunkGetter(char c1, char c2)
	{
		this.c1 = c1;
		this.c2 = c2;
	}
	
	public Vector<String> getStrippedShallowChunks(String input)
	{
		Vector<String> v = getShallowChunks(input);
		Vector<String> result = new Vector<String>();
		Iterator i = v.iterator();
		while(i.hasNext())
		{
			String temp = (String)i.next();
			String temp2 = temp.substring(1, temp.length()-1);
			result.add(temp2);
		}
		return result;
	}
	
	public Vector<String> getShallowChunks(String input)
	{
		Vector<String> result = new Vector<String>();
		int startIndex = input.indexOf(c1);
		int startOfChunk = startIndex;
		boolean inside = true;
		int numOpen = 1;
		int numClosed = 0;
		for(int a = startIndex + 1; a < input.length(); a++)
		{
			if(input.charAt(a) == c1)
			{
				numOpen++;
				if(inside == false)
				{
					startOfChunk = a;
				}
				inside = true;
			}
			else if(input.charAt(a) == c2)
				numClosed++;
			if(numOpen == numClosed && inside == true)
			{
				result.add(input.substring(startOfChunk, a + 1));
				startOfChunk = a + 1;
				inside = false;
			}		
		}
		
		return result;
	}
	public Vector<String> getAllChunks(String input)
	{
		Vector<String> result = new Vector<String>();
		Vector<String> subResult = getShallowChunks(input);
		result.addAll(subResult);
		if(subResult.isEmpty())
			return subResult;
		else
		{
			Iterator<String> i = subResult.iterator();
			while(i.hasNext())
			{
				String temp = i.next();
				String temp2 = temp.substring(1, temp.length()-1);
				result.addAll(getAllChunks(temp2));
			}
		}
		return result;
	}
	public Vector<String> getDeepChunks(String input)
	{
		Vector<String> v = getDeepChunksHelper(input);
		Vector<String> result = new Vector<String>();
		for(Iterator<String> i = v.iterator();i.hasNext();)
		{
			result.add(c1 + i.next().trim() + c2);
		}
		return result;
	}
	
	public Vector<String> getDeepChunksHelper(String input)
	{
		Vector<String> result = new Vector<String>();
		result.add(input);
		Vector<String> subResult = getShallowChunks(input);
		if(!subResult.isEmpty())
		{
			result.remove(input);
		}

		Iterator<String> i = subResult.iterator();
		while(i.hasNext())
		{
			String temp = i.next();
			String temp2 = temp.substring(1, temp.length()-1);
			result.addAll(getDeepChunksHelper(temp2));
		}
		return result;
	}
	
	public String getFirstChunk(String input)
	{
		Vector v = getShallowChunks(input);
		return (String) v.firstElement();
	}
}
